package com.example.firstfive;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import java.util.List;
import android.os.Handler;
import android.widget.ProgressBar;

public class RateListActivity extends AppCompatActivity implements AdapterView.OnItemLongClickListener {
    Handler handler;
    private ProgressBar bar;
    ListView listView;
    private String logDate = "";//用于保存从SP里获得数据
    private final String DATE_SP_KEY = "lastRateDateStr";//用于记录保存在SP中的数据KEY，
    // 在onCreate方法里首先需要获取SP中的日期记录，放在super.onCreate(...)之后

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rate_list);

        ListView mylist = findViewById(R.id.lv);
          String data[] = {"one", "two", "three", "测试数据"};
        ListAdapter adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, data);
         mylist.setAdapter(adapter);
        bar=findViewById(R.id.progressBar);
        listView.setOnItemLongClickListener(this);

        handler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                //super.handleMessage(msg);
                //收到数据
                if (msg.what == 7) {
                    List<String> list = (List<String>) msg.obj;
                    //用户刷新列表
                    ListAdapter adapter = new ArrayAdapter<String>(RateListActivity.this,
                            android.R.layout.simple_list_item_1, list);
                    mylist.setAdapter(adapter);
                    //yingcang
                    bar.setVisibility(View.INVISIBLE);//
                }
                super.handleMessage(msg);
                //
                SharedPreferences sp = getSharedPreferences("myrate", Context.MODE_PRIVATE);
                logDate = sp.getString(DATE_SP_KEY, "");
                Log.i("List","lastRateDateStr=" + logDate);

            }
        };
        //开启子线程
        MapTask mapTask = new MapTask();
        mapTask.setHandler(handler);
        Thread t = new Thread(mapTask);
        t.start();
    }


    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
        //长安删除

        return true;
    }
}

